﻿using MediatR;
using WeekWork929.Api.Read.Appliaction.Commond;
using WeekWork929.Api.Read.Dto;
using WeekWork929.Domain;
using WeekWork929.ErrorCode;
using WeekWork929.infrastrunceture;

namespace WeekWork929.Api.Read.Appliaction.CommondHandler
{
    public class ShowWorkCommondHandler : IRequestHandler<ShowWorkCommond, ApiResult<ApiPaging<WorkDto>>>
    {
        private readonly IBaseIRepository<Workmend> workMed;

        public ShowWorkCommondHandler(IBaseIRepository<Workmend> workMed)
        {
            this.workMed = workMed;
        }

        public Task<ApiResult<ApiPaging<WorkDto>>> Handle(ShowWorkCommond request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<WorkDto>> result = new ApiResult<ApiPaging<WorkDto>>();
            result.Code=ApiEnum.查询成功;
            result.Msg=ApiEnum.查询成功.ToString();
            var list = from a in workMed.GetAll()
                       where a.Isdelete == false
                       select new WorkDto()
                       {
                           WorkmendId=a.WorkmendId, 
                           WorkmendName=a.WorkmendName,
                           Number=a.Number,
                           WorkmendStatus=a.WorkmendStatus,
                           Desc=a.Desc,
                       };
            var totoCount = list.Count();
            var totoPage = (int)Math.Ceiling(totoCount * 1.0 / request.Pagesize);
            list = list.OrderBy(x => x.Number).Skip((request.Pageindex - 1) * request.Pagesize).Take(request.Pagesize);
            ApiPaging<WorkDto> paging = new ApiPaging<WorkDto>();
            paging.totoPage = totoPage;
            paging.totoCount = totoCount;
            paging.data = list.ToList();
            result.data = paging;
            return Task.FromResult(result);
        }
    }
}
